﻿#include "BugReportDialog.h"
#include "ui_BugReportDialog.h"

#include "components/Loading/Loading.h"

#include <FMessageBox/FMessageBox.h>
#include <FButtons/FPrimaryButton.h>
#include <FTag/FTag.h>

#include <QFileDialog>

#define RECEIVER_MAIL "656196136@qq.com"

#define MAX_ATTACHMENT_CNT      5
#define MAX_ATTACHMENT_SIZE_MB  5

BugReportDialog::BugReportDialog(QWidget *parent)
    : FDialog(parent)
    , ui(new Ui::BugReportDialog)
{
    ui->setupUi(this);
    setWindowTitle(tr("Bug Report"));
    setWindowIcon(QIcon(":/imgs/bug.png"));

    FPrimaryButton* btnAdd = new FPrimaryButton(this);
    btnAdd->setText(tr("Add Attachment"));
    connect(btnAdd, &FPrimaryButton::clicked, this, &BugReportDialog::onAddAttachment);
    ui->rteMailBody->addWidget(btnAdd);
}

BugReportDialog::~BugReportDialog()
{
    delete ui;
}

void BugReportDialog::onAddAttachment()
{
    FMessageBox::warning(this, tr("Warning"), tr("This feature is not yet implemented!"));

    QStringList files = QFileDialog::getOpenFileNames(this, tr("Select Attachment)"));
    if (files.isEmpty()) return;
    // 附件数量太多（大于5）
    if (files.length() + ui->layoutAttachments->count() > MAX_ATTACHMENT_CNT) {
        FMessageBox::warning(this, tr("Warning"), tr("Add up to 5 attachments!"));
        return;
    }

    qint64 totalSize = 0;
    for (int i = 0; i < ui->layoutAttachments->count(); i++) {
        FFileTag* tag = qobject_cast<FFileTag*>(ui->layoutAttachments->itemAt(i)->widget());
        totalSize += tag->fileSize();
    }
    for (auto& file: files) {
        totalSize += QFile(file).size();
    }
    // 总大小大于 5MB
    if (totalSize > MAX_ATTACHMENT_SIZE_MB * 1024 * 1024) {
        FMessageBox::warning(this, tr("Warning"), tr("The total size cannot exceed 5MB"));
        return;
    }

    for (auto& file: files) {
        FFileTag* tag = new FFileTag(file, Qt::green, this);
        tag->adjustSize();
        tag->setFixedSize(tag->size());
        ui->layoutAttachments->addWidget(tag);
    }
}

void BugReportDialog::on_btnSubmit_clicked()
{
    // 设置邮件信息
    m_sender.setSubject(ui->leSubject->text());  // 主题
    m_sender.setTitle("Bug Report");
    m_sender.setReceiverMail(RECEIVER_MAIL);  // 接收者邮箱
    m_sender.setHtml(ui->rteMailBody->toHtml());  // 内容html
    m_sender.clearAttachments();  // 附件
    for (int i = 0; i < ui->layoutAttachments->count(); i++) {
        FFileTag* tag = qobject_cast<FFileTag*>(ui->layoutAttachments->itemAt(i)->widget());
        QByteArray fc = tag->getFileContent();
        m_sender.addAttachment(tag->filename(), fc);
    }
    m_sender.send();  // 发送邮箱

    Loading* loading = new Loading(this, tr("Sending..."));
    connect(&m_sender, &MailSender::finished, this, [=](bool ok, const QString& msg){
        m_sender.disconnect(this);
        loading->stop();
        if (ok) {  // 发送成功
            FMessageBox::information(this, tr("Info"), tr("Submission Successful"));
        } else {  // 发送失败
            QString text = tr("Submission Failed");
            text += "\n" + msg;
            FMessageBox::critical(this, tr("Error"), text);
        }
    });
    loading->start();
}

void BugReportDialog::on_btnCancel_clicked()
{
    reject();
}
